package com.example.drive.fragment

import android.app.Dialog
import android.content.Context
import com.example.drive.base.BaseDialogFragment
import android.view.WindowManager
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.example.drive.*
import com.example.drive.databinding.FragmentAnswerDetailBinding
import java.util.*

/**
 * 答题详情
 */
class AnswerDetailFragment(private var onDrivingDialogReadyListener: () -> Unit) :
    BaseDialogFragment<FragmentAnswerDetailBinding>() {
    private var drivingAnswerAdapter: DrivingAnswerAdapter1? = null
    private lateinit var mContext: Context

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
    }

    override fun initDialog(): Dialog {
        val dialog = Dialog(mContext, R.style.DrivingAnswerDialog)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.fragment_answer_detail)
        // 生命周期方法:onCreateDialog先于onCreateView,不能用以下方法设置
//        dialog.setContentView(binding?.getRoot());
        dialog.setCanceledOnTouchOutside(true)
        // 设置弹出框布局参数,宽度铺满全屏底部
        val window = dialog.window
        val layoutParams = window?.attributes
        layoutParams?.gravity = Gravity.BOTTOM
        layoutParams?.width = WindowManager.LayoutParams.MATCH_PARENT
        window?.attributes = layoutParams
        return dialog
    }

    override fun initData() {
        val mainFragment = parentFragment as? MainFragment
        drivingAnswerAdapter = DrivingAnswerAdapter1(
            mContext,
            listOf(),
            R.layout.driving_answer_item,
            object : RecyclerViewCommonAdapter.OnItemListener {
                override fun onItemClick(view: View, position: Int) {
                    mainFragment?.onPageSelected(position)
                    dismiss()
                }

                override fun onItemLongClick(view: View, position: Int) {
                    Toast.makeText(mContext, "position = $position", Toast.LENGTH_SHORT).show()
                }
            })
        binding?.recyclerView?.layoutManager = GridLayoutManager(mContext, 5)
        binding?.recyclerView?.adapter = drivingAnswerAdapter
        binding?.tvBack?.setOnClickListener { dismiss() }
        // 准备好了,宿主Fragment可以刷新数据了
        onDrivingDialogReadyListener.invoke()

        // 统计答题情况
        binding?.tvRight?.text = "正确:${getAnswerResult(DrivingAnswer.RIGHT)}"
        binding?.tvWrong?.text = "错误:${getAnswerResult(DrivingAnswer.WRONG)}"
        binding?.tvUnfinished?.text = "未完成:${getAnswerResult(DrivingAnswer.UNFINISHED)}"
    }

    fun refreshData(linkedHashMap: LinkedHashMap<Int, DrivingAnswer>) {
        val list: List<DrivingAnswer> = ArrayList(linkedHashMap.values)
        drivingAnswerAdapter?.refreshData(list)
    }

    private fun getAnswerResult(result: DrivingAnswer): Int {
        return drivingAnswerAdapter?.dataList?.filter {
            it == result
        }?.size ?: 0
    }
}